package com.wdb007.venus.svc.biz.utils;

import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import javax.imageio.ImageIO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ObjectUtils;

import com.aliyun.oss.ClientConfiguration;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class ShareImageUtil {

	private static Logger logger = LoggerFactory.getLogger(ShareImageUtil.class);

	private static Font font = null;

	public static String FONT_PATH = "/simhei.ttf";
	private static float FONT_SIZE = 25;

	static {
		font = FontUtil.loadFont(FONT_PATH, FONT_SIZE);
	}

	public static void initFont(String fontPath, float size) {
		font = FontUtil.loadFont(fontPath, size);
	}

	public static class TextOptions {
		private String text;
		private int textX;
		private int textY;
		private int textWidth;
		private int textHeight;
		private int textMaxLine;
		private int textLineSpace;
		private Color fontColor;
		private Float fontSize;

		public void setFontSize(Float fontSize) {
			this.fontSize = fontSize;
		}

		public Float getFontSize() {
			return fontSize;
		}

		public String getText() {
			return text;
		}

		public void setText(String text) {
			this.text = text;
		}

		public int getTextX() {
			return textX;
		}

		public void setTextX(int textX) {
			this.textX = textX;
		}

		public int getTextY() {
			return textY;
		}

		public void setTextY(int textY) {
			this.textY = textY;
		}

		public int getTextWidth() {
			return textWidth;
		}

		public void setTextWidth(int textWidth) {
			this.textWidth = textWidth;
		}

		public int getTextHeight() {
			return textHeight;
		}

		public void setTextHeight(int textHeight) {
			this.textHeight = textHeight;
		}

		public int getTextMaxLine() {
			return textMaxLine;
		}

		public void setTextMaxLine(int textMaxLine) {
			this.textMaxLine = textMaxLine;
		}

		public int getTextLineSpace() {
			return textLineSpace;
		}

		public void setTextLineSpace(int textLineSpace) {
			this.textLineSpace = textLineSpace;
		}

		public Color getFontColor() {
			return fontColor;
		}

		public void setFontColor(Color fontColor) {
			this.fontColor = fontColor;
		}

	}

	public static class ImageOptions {

		private String url;
		private int imageX;
		private int imageY;
		private int imageWidth;
		private int imageHeight;

		public String getUrl() {
			return url;
		}

		public void setUrl(String url) {
			this.url = url;
		}

		public int getImageX() {
			return imageX;
		}

		public void setImageX(int imageX) {
			this.imageX = imageX;
		}

		public int getImageY() {
			return imageY;
		}

		public void setImageY(int imageY) {
			this.imageY = imageY;
		}

		public int getImageWidth() {
			return imageWidth;
		}

		public void setImageWidth(int imageWidth) {
			this.imageWidth = imageWidth;
		}

		public int getImageHeight() {
			return imageHeight;
		}

		public void setImageHeight(int imageHeight) {
			this.imageHeight = imageHeight;
		}

	}

	public static class ImageOptionsBuilder {

		private String templatePath;
		private String text;
		private int textX;
		private int textY;
		private int textWidth;
		private int textHeight;
		private int textMaxLine;
		private int textLineSpace;
		private Color fontColor;
		private int qrCodeSize;
		private int qrX;
		private int qrY;
		private String qrCodeURl;
		private String targetPath;
		private List<TextOptions> texts;
		private List<ImageOptions> images;

		public void setImages(List<ImageOptions> images) {
			this.images = images;
		}

		public List<ImageOptions> getImages() {
			return images;
		}

		public void setTexts(List<TextOptions> texts) {
			this.texts = texts;
		}

		public List<TextOptions> getTexts() {
			return texts;
		}

		public String getTemplatePath() {
			return templatePath;
		}

		public void setTemplatePath(String templatePath) {
			this.templatePath = templatePath;
		}

		public String getText() {
			return text;
		}

		public void setText(String text) {
			this.text = text;
		}

		public int getTextX() {
			return textX;
		}

		public void setTextX(int textX) {
			this.textX = textX;
		}

		public int getTextY() {
			return textY;
		}

		public void setTextY(int textY) {
			this.textY = textY;
		}

		public int getTextWidth() {
			return textWidth;
		}

		public void setTextWidth(int textWidth) {
			this.textWidth = textWidth;
		}

		public int getTextHeight() {
			return textHeight;
		}

		public void setTextHeight(int textHeight) {
			this.textHeight = textHeight;
		}

		public int getTextMaxLine() {
			return textMaxLine;
		}

		public void setTextMaxLine(int textMaxLine) {
			this.textMaxLine = textMaxLine;
		}

		public Color getFontColor() {
			return fontColor;
		}

		public void setFontColor(Color fontColor) {
			this.fontColor = fontColor;
		}

		public int getQrX() {
			return qrX;
		}

		public void setQrX(int qrX) {
			this.qrX = qrX;
		}

		public int getQrY() {
			return qrY;
		}

		public void setQrY(int qrY) {
			this.qrY = qrY;
		}

		public String getTargetPath() {
			return targetPath;
		}

		public void setTargetPath(String targetPath) {
			this.targetPath = targetPath;
		}

		public int getTextLineSpace() {
			return textLineSpace;
		}

		public void setTextLineSpace(int textLineSpace) {
			this.textLineSpace = textLineSpace;
		}

		public int getQrCodeSize() {
			return qrCodeSize;
		}

		public void setQrCodeSize(int qrCodeSize) {
			this.qrCodeSize = qrCodeSize;
		}

		public void setQrCodeURl(String qrCodeURl) {
			this.qrCodeURl = qrCodeURl;
		}

		public String getQrCodeURl() {
			return qrCodeURl;
		}

	}

	public static String createShareImage(ImageOptionsBuilder iob) {

		try {
			// 生成二维码
			BufferedImage qrImage = null;
			if(!ObjectUtils.isEmpty(iob.getQrCodeURl()) ) {//有链接则生成二维码
				qrImage = createQrImage(iob.getQrCodeURl(), null, iob.getQrCodeSize(), 0, true);
			}
			
			Image srcImg = ImageIO.read(new File(iob.getTemplatePath()));//生成图片
			BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null), srcImg.getHeight(null),
					BufferedImage.TYPE_INT_RGB);

			// 得到画笔对象
			Graphics2D g = buffImg.createGraphics();
			// 设置对线段的锯齿状边缘处理
			g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
			g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), Image.SCALE_SMOOTH), 0,
					0, null);
			if(qrImage != null) {
				g.drawImage(qrImage, iob.getQrX(), iob.getQrY(), null);
			}
			g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));

			if (iob.getText() != null) {
				// 设置颜色
				g.setColor(iob.getFontColor());
				// 设置 Font
				if (font == null) {
					font = FontUtil.loadFont(FONT_PATH, FONT_SIZE);
				}
				g.setFont(font);
				g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

				char[] charData = iob.getText().toCharArray();
				int textAreaWidth = 0;
				int textX = iob.getTextX();
				int textY = iob.getTextY();
				int textWidth = iob.getTextWidth();
				int textHeight = iob.getTextHeight();
				int maxLine = iob.getTextMaxLine();
				int textLineSpace = iob.getTextLineSpace();

				int curLine = 0;
				String textLine = "";
				for (int i = 0; i < charData.length; i++) {
					int charWidth = g.getFontMetrics().charWidth(charData[i]);
					if (textAreaWidth + charWidth <= textWidth) {
						textAreaWidth += charWidth;
						textLine += String.valueOf(charData[i]);
						if (charData.length - 1 == i || charData[i] == '\n') {
							curLine++;
							if (curLine <= maxLine) {
								g.drawString(textLine, textX, textY + curLine * textLineSpace);
							}
						}
					} else {
						curLine++;
						if (curLine <= maxLine) {
							g.drawString(textLine, textX, textY + curLine * textLineSpace);
						}
						textLine = String.valueOf(charData[i]);
						textAreaWidth = charWidth;
					}
				}
			}

			if (iob.getTexts() != null) {
				for (TextOptions textOps : iob.getTexts()) {
					// 设置颜色
					g.setColor(textOps.getFontColor());
					// 设置 Font
					if (font == null) {
						font = FontUtil.loadFont(FONT_PATH, FONT_SIZE);
					}
					g.setFont(font);
					if (textOps.getFontSize() != null) {
						Font font = FontUtil.loadFont(FONT_PATH, textOps.getFontSize());
						g.setFont(font);
					}
					// g.setFont(new Font("黑体", Font.BOLD, 25));
					g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

					char[] charData = textOps.getText().toCharArray();
					int textAreaWidth = 0;
					int textX = textOps.getTextX();
					int textY = textOps.getTextY();
					int textWidth = textOps.getTextWidth();
					int textHeight = textOps.getTextHeight();
					int maxLine = textOps.getTextMaxLine();
					int textLineSpace = textOps.getTextLineSpace();

					int curLine = 0;
					String textLine = "";
					for (int i = 0; i < charData.length; i++) {
						int charWidth = g.getFontMetrics().charWidth(charData[i]);
						if (textAreaWidth + charWidth <= textWidth) {
							textAreaWidth += charWidth;
							textLine += String.valueOf(charData[i]);
							if (charData.length - 1 == i || charData[i] == '\n') {
								curLine++;
								if (curLine <= maxLine) {
									g.drawString(textLine, textX, textY + curLine * textLineSpace);
								}
							}
						} else {
							curLine++;
							if (curLine <= maxLine) {
								g.drawString(textLine, textX, textY + curLine * textLineSpace);
							}
							textLine = String.valueOf(charData[i]);
							textAreaWidth = charWidth;
						}
					}
				}
			}

			if (iob.getImages() != null) {
				for (ImageOptions io : iob.getImages()) {
					
					BufferedImage bi = getImageFromNetByUrl(io.getUrl());
					bi = resize(bi, io.getImageWidth(), io.getImageHeight());
					bi = getCircleImage(bi);
					
					g.drawImage(bi, io.getImageX(), io.getImageY(), null);
				}
			}

			g.dispose();
			// // 生成图片
			// ImageIO.write(buffImg, "JPG", os);
			System.out.println("分享图片已生成");

			ByteArrayOutputStream out = new ByteArrayOutputStream();
			boolean flag = ImageIO.write(buffImg, "JPG", out);
			byte[] data = out.toByteArray();

			ClientConfiguration config = new ClientConfiguration();
			OSSClient client = new OSSClient("http://oss-cn-shanghai.aliyuncs.com/", "LTAI3FIgF26XglMg",
					"blEXM575lLjKYHpbclm0t4fmdCxz8l", config);
			ObjectMetadata objectMeta = new ObjectMetadata();
			objectMeta.setContentLength(data.length);
			objectMeta.setContentType("image/jpeg");

			InputStream input = new ByteArrayInputStream(data);
			client.putObject("wdb-html", iob.getTargetPath(), input, objectMeta);
			return "https://sm.wdb007.com/" + iob.getTargetPath()+"?ts="+ System.currentTimeMillis();
		} catch (Exception e) {
			logger.info("share image create fail --!");
			logger.error(e.getMessage(), e);
		}
		return null;
	}

	private static final String CHARSET = "utf-8";
	private static final String FORMAT = "JPG";

	/**
	 * 生成二维码图片
	 * 
	 * @param content
	 * @param logoPath
	 * @param qrSize
	 * @param logoSize
	 * @param needCompress
	 * @return
	 * @throws Exception
	 */
	public static BufferedImage createQrImage(String content, String logoPath, int qrSize, int logoSize,
			boolean needCompress) throws Exception {
		Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
		hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
		hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
		hints.put(EncodeHintType.MARGIN, 1);
		BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, qrSize, qrSize, hints);
		int width = bitMatrix.getWidth();
		int height = bitMatrix.getHeight();
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
		for (int x = 0; x < width; x++) {
			for (int y = 0; y < height; y++) {
				image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0x00000000);
			}
		}
		if (logoPath == null || "".equals(logoPath)) {
			return image;
		}
		// 插入图片
		if (logoPath != null) {
			insertImage(image, logoPath, null,logoSize, qrSize, needCompress);
		}
		return image;
	}

	/**
	 * 图片中插入别的图片
	 * 
	 * @param source
	 * @param logoPath
	 * @param iconSize
	 * @param qrSize
	 * @param needCompress
	 * @throws Exception
	 */
	private static void insertImage(BufferedImage source, String logoPath, Image src, int iconSize, int qrSize,
			boolean needCompress) throws Exception {
		if(logoPath != null) {
			File file = new File(logoPath);
			if (!file.exists()) {
				throw new Exception("logo file not found.");
			}
			 src = ImageIO.read(new File(logoPath));
		}
		
		int width = src.getWidth(null);
		int height = src.getHeight(null);
		if (needCompress) { // 压缩LOGO
			if (width > iconSize) {
				width = iconSize;
			}
			if (height > iconSize) {
				height = iconSize;
			}
			Image image = src.getScaledInstance(width, height, Image.SCALE_SMOOTH);
			BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
			Graphics g = tag.getGraphics();
			g.drawImage(image, 0, 0, null); // 绘制缩小后的图
			g.dispose();
			src = image;
		}
		// 插入LOGO
		Graphics2D graph = source.createGraphics();
		int x = (qrSize - width) / 2;
		int y = (qrSize - height) / 2;
		graph.drawImage(src, x, y, width, height, null);
		Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
		graph.setStroke(new BasicStroke(3f));
		graph.draw(shape);
		graph.dispose();
	}

	/**
	 * 
	 * @param readNum 1
	 * @param ranking 第14名
	 * @param nextReadNum 再读 1本
	 * @param rankTitle 阅读小老鼠
	 * @param headerUrl 头像地址
	 * @return
	 */
	public static String getShareImgUrl(String readNum, String ranking, String nextReadNum,String rankTitle,Long userid,String headerUrl) {
		logger.info("user rank "+userid+","+readNum + ranking + nextReadNum + rankTitle);
		//背景图
        String srcImgPath = "/share.png";
        //上传oss路径
		String targerPath = "share/"
				+ userid
				+ ".jpg";

		initFont(FONT_PATH, 18);

		ImageOptionsBuilder iob = new ImageOptionsBuilder();
//		iob.setFontColor(new Color(221,221,221));
//		iob.setQrCodeSize(114);
//		iob.setQrCodeURl("");
//		iob.setQrX(313);
//		iob.setQrY(446);
		iob.setTargetPath(targerPath);
		iob.setTemplatePath(srcImgPath);

		String name = "宝贝已阅读了";
		List<TextOptions> tos = new ArrayList<TextOptions>();
		TextOptions to1 = new TextOptions();
		to1.setText(name);
		to1.setFontColor(new Color(67,67,66));
		to1.setTextHeight(100);
		to1.setTextLineSpace(30);
		to1.setTextMaxLine(3);
		to1.setTextWidth(160);
		to1.setTextX(83);
		to1.setTextY(133);
		to1.setFontSize(24f);
		tos.add(to1);
		
		//1本
		TextOptions to1num = new TextOptions();
		to1num.setText(readNum+"本");
		to1num.setFontColor(new Color(254,204,14));
		to1num.setTextHeight(100);
		to1num.setTextLineSpace(30);
		to1num.setTextMaxLine(3);
		to1num.setTextWidth(50);
		to1num.setTextX(83+150);
		to1num.setTextY(133);
		to1num.setFontSize(24f);
		tos.add(to1num);
		//童书
		TextOptions to1text = new TextOptions();
		to1text.setText("童书");
		to1text.setFontColor(new Color(67,67,66));
		to1text.setTextHeight(100);
		to1text.setTextLineSpace(30);
		to1text.setTextMaxLine(3);
		to1text.setTextWidth(50);
		to1text.setTextX(83+165+45);
		to1text.setTextY(133);
		to1text.setFontSize(24f);
		tos.add(to1text);
		
		TextOptions to2 = new TextOptions();
		to2.setText("书虫圈排名");
		to2.setFontColor(new Color(67,67,66));
		to2.setTextHeight(100);
		to2.setTextLineSpace(30);
		to2.setTextMaxLine(3);
		to2.setTextWidth(120);
		to2.setTextX(107);
		to2.setTextY(162);
		to2.setFontSize(24f);
		tos.add(to2);
		
		TextOptions to2Num = new TextOptions();
		to2Num.setText("第"+ranking+"名");
		to2Num.setFontColor(new Color(254,204,14));
		to2Num.setTextHeight(100);
		to2Num.setTextLineSpace(30);
		to2Num.setTextMaxLine(3);
		to2Num.setTextWidth(670);
		to2Num.setTextX(107+120);
		to2Num.setTextY(162);
		to2Num.setFontSize(24f);
		tos.add(to2Num);
		//再看1本就能获得“阅读小老鼠”称号////////
		TextOptions to3 = new TextOptions();
		to3.setText("再看");
		to3.setFontColor(new Color(160,160,160
));
		to3.setTextHeight(100);
		to3.setTextLineSpace(30);
		to3.setTextMaxLine(3);
		to3.setTextWidth(36);
		to3.setTextX(42);
		to3.setTextY(192);
		to3.setFontSize(18f);
		tos.add(to3);
		
		TextOptions to3Num = new TextOptions();
		to3Num.setText(""+nextReadNum+"本");
		to3Num.setFontColor(new Color(254,204,14));
		to3Num.setTextHeight(100);
		to3Num.setTextLineSpace(30);
		to3Num.setTextMaxLine(3);
		to3Num.setTextWidth(40);
		to3Num.setTextX(42+36);
		to3Num.setTextY(192);
		to3Num.setFontSize(18f);
		tos.add(to3Num);
		
		//就能获得“阅读小老鼠”称号
		
		TextOptions to3Text = new TextOptions();
		to3Text.setText(",就能获得“"
				+ rankTitle
				+ "”称号");
		to3Text.setFontColor(new Color(160,160,160
));
		to3Text.setTextHeight(100);
		to3Text.setTextLineSpace(30);
		to3Text.setTextMaxLine(3);
		to3Text.setTextWidth(340);
		to3Text.setTextX(42+34+34);
		to3Text.setTextY(192);
		to3Text.setFontSize(18f);
		tos.add(to3Text);

		iob.setTexts(tos);

		List<ImageOptions> ios = new ArrayList<ImageOptions>();
		ImageOptions io1 = new ImageOptions();
		io1.setUrl(headerUrl.replace("https://wx.qlogo.cn/", "http://wx.qlogo.cn/"));
		io1.setImageX(179);
		io1.setImageY(51);
		io1.setImageWidth(60);
		io1.setImageHeight(60);
		ios.add(io1);
		
		iob.setImages(ios);

		System.out.println(createShareImage(iob));
	
		return createShareImage(iob);
	}


	public static byte[] file2ByteArray(String filename) throws IOException {

		File f = new File(filename);
		if (!f.exists()) {
			throw new FileNotFoundException(filename);
		}

		ByteArrayOutputStream bos = new ByteArrayOutputStream((int) f.length());
		BufferedInputStream in = null;
		try {
			in = new BufferedInputStream(new FileInputStream(f));
			int buf_size = 1024;
			byte[] buffer = new byte[buf_size];
			int len = 0;
			while (-1 != (len = in.read(buffer, 0, buf_size))) {
				bos.write(buffer, 0, len);
			}
			return bos.toByteArray();
		} catch (IOException e) {
			e.printStackTrace();
			throw e;
		} finally {
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			bos.close();
		}
	}

	public static BufferedImage getCircleImage(BufferedImage bi1) {

		int size = bi1.getHeight();
		if(bi1.getWidth() < bi1.getHeight())
		{
			size = bi1.getWidth();
		}
		
		BufferedImage bi2 = new BufferedImage(bi1.getWidth(), bi1.getHeight(), BufferedImage.TYPE_INT_ARGB);

		Ellipse2D.Double shape = new Ellipse2D.Double(0, 0, size, size);

		Graphics2D g2 = bi2.createGraphics();
//		g2.setBackground(Color.WHITE);
		g2.setComposite(AlphaComposite.Clear);
		g2.fill(new Rectangle(bi2.getWidth(), bi2.getHeight()));
		g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, 1.0f));
		g2.setClip(shape);
		// 使用 setRenderingHint 设置抗锯齿
		g2.drawImage(bi1, 0, 0, null);
		g2.dispose();

		return bi2;
	}

	public static BufferedImage getImageFromNetByUrl(String strUrl) {
		try {
			URL url = new URL(strUrl);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("GET");
			conn.setConnectTimeout(5 * 1000);
			InputStream inStream = conn.getInputStream();// 通过输入流获取图片数据
			byte[] btImg = readInputStream(inStream);// 得到图片的二进制数据
			ByteArrayInputStream in = new ByteArrayInputStream(btImg);
			BufferedImage image = ImageIO.read(in);
			return image;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static byte[] readInputStream(InputStream inStream) throws Exception {
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = 0;
		while ((len = inStream.read(buffer)) != -1) {
			outStream.write(buffer, 0, len);
		}
		inStream.close();
		return outStream.toByteArray();
	}

	private static BufferedImage resize(BufferedImage source, int targetW, int targetH) {
		// targetW，targetH分别表示目标长和宽
		int type = source.getType();
		BufferedImage target = null;
		double sx = (double) targetW / source.getWidth();
		double sy = (double) targetH / source.getHeight();
		// 这里想实现在targetW，targetH范围内实现等比缩放。如果不需要等比缩放
		// 则将下面的if else语句注释即可
		if (sx < sy) {
			sx = sy;
			targetW = (int) (sx * source.getWidth());
		} else {
			sy = sx;
			targetH = (int) (sy * source.getHeight());
		}
		if (type == BufferedImage.TYPE_CUSTOM) { // handmade
			ColorModel cm = source.getColorModel();
			WritableRaster raster = cm.createCompatibleWritableRaster(targetW, targetH);
			boolean alphaPremultiplied = cm.isAlphaPremultiplied();
			target = new BufferedImage(cm, raster, alphaPremultiplied, null);
		} else
			target = new BufferedImage(targetW, targetH, type);
		Graphics2D g = target.createGraphics();
		// smoother than exlax:
		g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
		g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
		g.dispose();
		return target;
	}

	public static void main(String[] args) {
		 getShareImgUrl("21","1","44","阅读霸王龙",
		 12310L,"https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLAWzNxwFnic44gEWapShFzRO44Cibdx4j27QKfO21e3WXCYvqicjd2vbIBJ5yXm7kzUpEZ6ThyA3jOg/0");
		 
	}
}
